package GUI;
import org.jdesktop.jdic.tray.SystemTray;
import org.jdesktop.jdic.tray.TrayIcon;


import Buisness.*;
import Domain.Website;

import javax.swing.*;
import java.awt.event.*;

public class TrayApp implements ActionListener, ItemListener {
    
    SystemTray tray = SystemTray.getDefaultSystemTray();
    private TrayIcon ti;
    private StationsManager sm;
    private JPopupMenu trayMenu;
    private BrowserWindow webBrowser;
    private StationsEditor se;
    
    /* Constructor
     **************************/
    public TrayApp() {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        sm = new StationsManager();
        
        se = new StationsEditor(sm);
        se.addActionListener(this);
        
        webBrowser = new BrowserWindow(sm.getCurrentStation());
        
        trayMenu = new JPopupMenu("A Menu");
        initTrayMenu();
        
        String name = "fm.gif";
        java.net.URL vUrl = ClassLoader.getSystemResource(name);
        
        if (vUrl == null) {
            vUrl = getClass().getResource(name);
        }
        
        if (vUrl == null) {
            throw new RuntimeException( "Image" + " " + name + " " + "not found" );
        }
        ti = new TrayIcon(new javax.swing.ImageIcon( vUrl ), "Tray Radio", trayMenu);
        ti.setIconAutoSize(true);
        ti.addActionListener(this);
        tray.addTrayIcon(ti);
        
    }
    
    
    
    /*  initialize the tray menue Items
     ******************************************************/
    private void initTrayMenu(){
        JMenuItem menuItem;
        if( Integer.parseInt(System.getProperty("java.version").substring(2,3)) >=5 )
            System.setProperty("javax.swing.adjustPopupLocationToFit", "false");
        
        // create the stations trayMenu items
        for (int i = 0; i < sm.size(); i++) {
            menuItem = new JMenuItem(sm.getStation(i).getName());
            menuItem.addActionListener(this);
            trayMenu.add(menuItem);
        }
        
        trayMenu.addSeparator();
        
        menuItem = new JMenuItem("Edit Stations");
        menuItem.addActionListener(this);
        trayMenu.add(menuItem);
        
        
        // "Quit" trayMenu item
        menuItem = new JMenuItem("Quit");
        menuItem.addActionListener(this);
        trayMenu.add(menuItem);
        
    }
    
    // Returns just the class name -- no package info.
    protected String getClassName(Object o) {
        String classString = o.getClass().getName();
        int dotIndex = classString.lastIndexOf(".");
        
        return classString.substring(dotIndex + 1);
    }
    
    
    
    
    /* ActionListener implementation
     ***********************************************************/
    public void actionPerformed(ActionEvent e) {
        
        String actionCommand = e.getActionCommand();
        
        if (actionCommand.equalsIgnoreCase("Edit Stations")) {
            
            sm.SaveConfiguration();
            java.awt.EventQueue.invokeLater(se);
            //System.out.println("Edit Stations choosed");
            
        }else  if (actionCommand.equalsIgnoreCase("StationsListUpdated")) {
            sm.loadConfiguration();
            trayMenu.removeAll();
            initTrayMenu();
            
        }else  if (actionCommand.equalsIgnoreCase("Quit")) {
            //System.out.println("Quit trayMenu item selected!");
            sm.SaveConfiguration();
            //webBrowser.dispose();
            System.exit(0);
        }else if (actionCommand.equalsIgnoreCase("PressAction")) {
                webBrowser.setVisible(!webBrowser.isVisible());
                if (webBrowser.isVisible())
                    webBrowser.toFront();
        } else {
            Website st = sm.getStation(actionCommand);
            if (null!=st) {
                sm.setCurrentStation(st);
                webBrowser.playStation(st);
            } else {
                actionCommand = "Action event detected.       ActionCommand:"+ actionCommand + "\n" + "    Event source: "
                        + e.getSource() + " (an instance of " + getClassName(e.getSource()) + ")";
                System.out.println(actionCommand);
            }
        }
    }
    
    
      /* ItemListener implementation
       ***********************************************************/
    public void itemStateChanged(ItemEvent e) {
        JMenuItem source = (JMenuItem) (e.getSource());
        String s = "Item event detected." + "\n" + "    Event source: "
                + source.getText() + " (an instance of " + getClassName(source)
                + ")" + "\n" + "    New state: "
                + ((e.getStateChange() == ItemEvent.SELECTED)
                ? "selected"
                : "unselected");
        
        System.out.println(s);
    }
    
    
    public static void main(String[] args) {
        new TrayApp();
    }
    
    
    
}
